<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item label="设备图片" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-image-upload isMultiple v-decorator="['devicePicture']"></j-image-upload>
        </a-form-item>
        <a-form-item label="设备SN" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['deviceSn', validatorRules.deviceSn]" placeholder="请输入设备SN"></a-input>
        </a-form-item>
        <a-form-item label="设备名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['deviceName']" placeholder="请输入设备名称"></a-input>
        </a-form-item>
        <a-form-item label="设备规格" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['deviceGague']" placeholder="请输入设备规格"></a-input>
        </a-form-item>
        <a-form-item label="设备类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['deviceType']" placeholder="请输入设备类型"></a-input>
        </a-form-item>
        <a-form-item label="设备型号" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['deviceModel']" placeholder="请输入设备型号"></a-input>
        </a-form-item>
        <a-form-item label="维护模板" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-multi-select-tag type="list_multi" v-decorator="['query1',validatorRules.query1]"
                              :trigger-change="true" dictCode="facility_name_temp" placeholder="请选择维护模板" />
        </a-form-item>
        <a-form-item label="阶别" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['query2']" :trigger-change="true" dictCode="produce_grade" placeholder="请选择阶别"/>
        </a-form-item>
        <a-form-item label="文件编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['query3']" placeholder="请输入文件编码"></a-input>
        </a-form-item>
        <a-form-item label="有效标志" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="radio" v-decorator="['validToken']" :trigger-change="true" dictCode="yn" placeholder="请选择有效标志"/>
        </a-form-item>
        <a-form-item label="使用部门" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-select-depart v-decorator="['usedDepart']" multi/>
        </a-form-item>
        <a-form-item label="责任人" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['principal']" placeholder="请输入责任人"></a-input>
        </a-form-item>
        <a-form-item label="责任人2" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['principalTwo']" placeholder="请输入责任人2"></a-input>
        </a-form-item>
        <a-form-item label="责任人3" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['principalThree']" placeholder="请输入责任人3"></a-input>
        </a-form-item>
        <a-form-item label="设备状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['deviceState']" :trigger-change="true" dictCode="device_state" placeholder="请选择设备状态"/>
        </a-form-item>
        <a-form-item label="出厂日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择出厂日期" v-decorator="['factoryDate']" :trigger-change="true" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="购买日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择购买日期" v-decorator="['purchaseDate']" :trigger-change="true" style="width: 100%"/>
        </a-form-item>
        <a-form-item label="供应商" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-popup
            v-decorator="['supplier']"
            :trigger-change="true"
            org-fields="id,supplier_name"
            dest-fields="supplierId,supplier"
            code="supplier_search"
            @callback="popupCallback"/>
        </a-form-item>
        <a-form-item label="设备位置" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['deviceLocation']" placeholder="请输入设备位置"></a-input>
        </a-form-item>
        <a-form-item label="点检周期(天)" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['checkCycle']" placeholder="请输入点检周期(天)"></a-input>
        </a-form-item>
        <a-form-item label="设备文件" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-upload v-decorator="['deviceFile']" :trigger-change="true"></j-upload>
        </a-form-item>
        <a-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea v-decorator="['notes']" rows="4" placeholder="请输入备注"/>
        </a-form-item>

      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'
  import JUpload from '@/components/jeecg/JUpload'
  import JImageUpload from '@/components/jeecg/JImageUpload'
  import JSelectDepart from '@/components/jeecgbiz/JSelectDepart'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"
  import JMultiSelectTag from '@comp/dict/JMultiSelectTag'


  export default {
    name: "MesChiefdataDevicearchiveModal",
    components: {
      JDate,
      JUpload,
      JImageUpload,
      JSelectDepart,
      JDictSelectTag,
      JMultiSelectTag
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title:"操作",
        width:800,
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          deviceSn: {
            rules: [
              { required: true, message: '请输入设备SN!'},
              { validator: (rule, value, callback) => validateDuplicateValue('mes_chiefdata_devicearchive', 'device_sn', value, this.model.id, callback)},
            ]
          },
          query1:{
            rules: [
              { required: true, message: '请选择维护模板!'},
            ]
          }
        },
        url: {
          add: "/chiefdata/mesChiefdataDevicearchive/add",
          edit: "/chiefdata/mesChiefdataDevicearchive/edit",
        }
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'query1','query2','query3','createBy','createTime','devicePicture','deviceSn','deviceName','deviceGague','deviceType','deviceModel','validToken','usedDepart','principal','principalTwo','principalThree','deviceState','factoryDate','purchaseDate','supplier','deviceLocation','checkCycle','deviceFile','notes'))
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }

        })
      },
      handleCancel () {
        this.close()
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'query1','query2','query3','createBy','createTime','devicePicture','deviceSn','deviceName','deviceGague','deviceType','deviceModel','validToken','usedDepart','principal','principalTwo','principalThree','deviceState','factoryDate','purchaseDate','supplier','deviceLocation','checkCycle','deviceFile','notes'))
      },


    }
  }
</script>